package com.mijackstudio.typefacedemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MiJack on 2015/12/14.
 */
public class FontManager {
    private static final String TAG = "FontManager";
    private static FontManager instance;
    private static WeakReference<Context> reference;
    private static Typeface defaultType;

    public static FontManager init(Context context) {
        if (instance == null) {
            synchronized (FontManager.class) {
                instance = new FontManager(context);
            }
        }
        return instance;
    }

    static Map<String, Typeface> typefaceMap;

    private FontManager(Context context) {
        this.reference = new WeakReference(context);
        this.typefaceMap = new HashMap();
    }

    public Context getContext() {
        if (reference == null) {
            return null;
        }
        return reference.get();
    }

    public FontManager inflater(String key, String font) {
        Context context = getContext();
        if (context == null) {
            return this;
        }
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), font);
        typefaceMap.put(key, typeface);
        return this;
    }

    public static void changeFonts(Activity activity) {
        changeFonts((ViewGroup) activity.findViewById(android.R.id.content));
    }

    private static void changeFonts(ViewGroup viewGroup) {
        try {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View v = viewGroup.getChildAt(i);
                if (v instanceof ViewGroup) {
                    changeFonts((ViewGroup) v);
                } else if (v instanceof View) {
                    changeFonts(v);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 更换字体
     *
     * @param view
     * @Title: changeFonts
     */
    public static void changeFonts(View view) {
        try {
            if (view instanceof ViewGroup) {
                changeFonts((ViewGroup) view);
            }
            Typeface typeface = getTypefaceFormView(view);
            if (typeface == null) {
                return;
            }
            if (view instanceof TextView) {
                ((TextView) view).setTypeface(typeface);
            } else if (view instanceof Button) {
                ((Button) view).setTypeface(typeface);
            } else if (view instanceof Switch) {
                ((Switch) view).setTypeface(typeface);
            } else if (view instanceof EditText) {
                ((EditText) view).setTypeface(typeface);
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            // TODO
            e.printStackTrace();
        }

    }

    private static Typeface getTypefaceFormView(View view) {
        String tag = view.getTag().toString();
        if (!tag.startsWith("font:")) {
            return null;
        }
        String font = tag.substring("font:".length());
        return typefaceMap.get(font);
    }

    public void setDefaultType(Typeface defaultType) {
        this.defaultType = defaultType;
    }
}
